
import Vue from "vue";
import VueRouter from "vue-router";
// import store from "./../store";
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location, onResolve, onReject) {

  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)

  return originalPush.call(this, location).catch(err => err)

}

Vue.use(VueRouter);

let routes = []; // 路由数组 - 存放所有路由

function importAll(routerArr) {
  // 该函数用于将所有分区路由中的路由添加到路由数组
  routerArr.keys().forEach(key => {
    routes = routes.concat(routerArr(key).default)
  });
}

importAll(require.context(".", true, /\.routes\.js/));

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  linkActiveClass: "active",
  srcollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return {
        selector: to.hash
      }
    }
  },
  routes
});

router.beforeEach((to, form, next) => {

  // const requireAuth = to.matched[0].meta.requireAuth;
  // const userData = store.state.basic.userData || '';
  // if(requireAuth && !userData) {
  //   basicData.usreData().then(res=> {
  //     if(res == 200) {
  //       next();

  //     }else{
  //       router.replace({
  //         name: 'Failed',
  //         params: {
  //           text: '抱歉！获取用户信息失败'
  //         }
  //       })
  //     }
  //   });

  // }else {
  //   next();
  // }
  next();
});
export default router;